#include "login_page.h"
#include "home_page.h"
#include "page_manager.h"
#include "com/virt_kb/virt_kb.h"
#include "com/usermgr/usermgr.h"
#include "com/login/login.h"
#include "com/sysmgr/sysmgr.h"
#include "ui/bar/status_bar/status_bar.h"

static login_t *g_login = NULL;
static status_bar_t *g_status_bar = NULL;

extern virt_kb_t *g_virt_kb;

// 验证用户名和密码的回调函数
static bool verify_password(const char *username, const char *password)
{
    int user_type;
    if(usermgr_verify_password(username, password)) {
        // 更新系统管理模块中的用户信息
        if (usermgr_get_user_type(username, &user_type)) {
            sysmgr_set_user(username, password, user_type);
        }
        switch_to_page(PAGE_HOME);
        return true;
    }
    return false;
}

void create_login_screen(lv_obj_t * parent) {
    // 创建状态栏
    g_status_bar = status_bar_create(parent);
    if (!g_status_bar) {
        LV_LOG_ERROR("Failed to create status bar");
        return;
    }
    
    // 设置状态栏初始状态
    status_bar_set_wifi_state(g_status_bar, true);
    status_bar_set_battery_state(g_status_bar, 100);
    status_bar_set_notify_state(g_status_bar, false);
    
    // 创建登录组件
    g_login = login_create(parent, verify_password, g_virt_kb);
    if (!g_login) {
        LV_LOG_ERROR("Failed to create login component");
        return;
    }
}

